import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:get/get.dart';
import 'package:tin_flutter/app/res/intl.dart';
import 'package:tin_flutter/app/routes.dart';

import '../../../../app/res/app_color.dart';
import 'brand_recommend_logic.dart';

/// * 作者: Tao
/// * 日期: 2025/11/10
/// * 时间: 16:42
/// * 描述: 品牌推荐页面
class BrandRecommendPage extends StatefulWidget {
  const BrandRecommendPage({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _BrandRecommendPageState();
}

class _BrandRecommendPageState extends State<BrandRecommendPage> {
  late ScrollController _scrollController;
  final logic = Get.find<BrandRecommendLogic>();

  @override
  void initState() {
    super.initState();
    _scrollController = ScrollController();
    _scrollController.addListener(_scrollListener);
    logic.getBrandList(isRefresh: true);
  }

  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }

  void _scrollListener() {
    // 当滚动到距离底部一定距离时触发加载
    if (_scrollController.position.extentAfter < 10 &&
        !logic.state.isLoading.value &&
        _scrollController.position.userScrollDirection == ScrollDirection.reverse) {
      logic.onLoadMore();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColor.grey,
      appBar: AppBar(
        backgroundColor: AppColor.grey,
        toolbarHeight: 44,
        leading: IconButton(
          icon: Icon(Icons.arrow_back, size: 20.0),
          onPressed: () {
            Get.back();
          },
        ),
        centerTitle: true,
        title:
            Text(Intl().brandRecommendTitle, style: TextStyle(color: AppColor.black, fontSize: 14)),
      ),
      body: SingleChildScrollView(
          controller: _scrollController,
          child: Padding(
              padding: EdgeInsets.only(left: 13.0, right: 13.0, bottom: 13.0),
              child: Column(
                children: [
                  // 轮播图
                  _buildBanner(),
                  SizedBox(height: 10),
                  // 品牌推荐列表
                  _buildBrandList(logic),
                ],
              ))),
    );
  }

  Widget _buildBanner() {
    return ClipRRect(
        borderRadius: BorderRadius.circular(10),
        child: Image(
          image: AssetImage('assets/images/recommend_brand_banner.png'),
          fit: BoxFit.cover,
        ));
  }

  Widget _buildBrandList(BrandRecommendLogic logic) {
    return Obx(() {
      return Container(
          decoration: BoxDecoration(
            color: AppColor.white,
            borderRadius: BorderRadius.circular(10),
          ),
          child: Padding(
              padding: EdgeInsets.only(left: 5.0, right: 5.0, top: 10.0, bottom: 10.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Text(Intl().brandRecommendRelated),
                  SizedBox(height: 10),
                  // 列表
                  GridView.builder(
                    shrinkWrap: true,
                    physics: NeverScrollableScrollPhysics(),
                    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 2,
                      mainAxisSpacing: 5.0,
                      crossAxisSpacing: 5.0,
                      childAspectRatio: 0.8,
                    ),
                    itemCount: logic.state.brandList.length,
                    itemBuilder: (context, index) {
                      return _buildBrandItem(index);
                    },
                  ),
                  SizedBox(height: 8.0),
                  Text(
                    !logic.state.hasMore.value
                        ? '没有更多数据了'
                        : (logic.state.isLoading.value ? '正在加载...' : '上拉加载更多'),
                    style: TextStyle(fontSize: 12.0, color: AppColor.grey500),
                  )
                ],
              )));
    });
  }

  /// 品牌制造商item
  /// * [image] logo
  /// * [label] 品牌名
  /// * [productCount] 数量
  Widget _buildBrandItem(int index) {
    return InkWell(
      onTap: () {
        Get.toNamed(Routes.brandDetail, arguments: logic.state.brandList[index].id);
      },
      child: Container(
        decoration: BoxDecoration(
          color: AppColor.white,
          borderRadius: BorderRadius.all(Radius.circular(5.0)),
          boxShadow: [
            BoxShadow(
              color: AppColor.grey ?? Colors.grey,
              offset: Offset(0.0, 0.5),
              blurRadius: 5.0,
              spreadRadius: 1.0,
            )
          ],
        ),
        child: Padding(
          padding: EdgeInsets.all(12.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Image.network(logic.state.brandList[index].logo, fit: BoxFit.cover),
              Spacer(),
              Text(logic.state.brandList[index].name,
                  style: TextStyle(fontSize: 13.0), maxLines: 1, overflow: TextOverflow.ellipsis),
              SizedBox(height: 4.0),
              Text('${Intl().brandCount}${logic.state.brandList[index].productCount}',
                  style: TextStyle(fontSize: 10.0, color: AppColor.grey500)),
            ],
          ),
        ),
      ),
    );
  }
}
